home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d886.lha / TextPort / source / AddCR.c next >
C/C++ Source or Header  |  1993-07-16  |  3KB  |  119 lines

  1. /*-------------------------------------------------------------+
  2.  | addcr.c -- 5/16/1992 public domain                          |
  3.  |            by Alex Matulich, Unicorn Research Corporation   |
  4.  | Add a CR character before each LF character in a text file. |
  5.  | Nothing is done where the CR+LF combination already exists. |
  6.  +-------------------------------------------------------------*/
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11.  
  12. #ifdef __ZTC__
  13. #define stricmp(a,b) strcmpl(a,b)   /* Zortech's case-insensitive strcmp() */
  14. #endif
  15. #define LF '\x0A'
  16. #define CR '\x0D'
  17.  
  18. long OUTBUFSIZ = 16384;
  19.  
  20. unsigned short replace, bufptr = 0;
  21. signed char *buf = NULL;
  22. FILE *infile, *outfile;
  23. long inpos = 0L, outpos = 0L;       /* file position trackers */
  24.  
  25. int writebuf(void);
  26.  
  27.  
  28. void main(int argc, char *argv[])
  29. {
  30. signed char ch[2];
  31. short plast = 0, p, err;
  32.  
  33. if( argc != 3 && argc != 2) {
  34.    puts("AddCR by Unicorn Research Corporation\nConvert LF end-of-line codes to CR+LF.\n\nUsage is:  AddCR infile [outfile]\n\nIf outfile is omitted, infile will be replaced.\nExisting CR+LF codes are not affected.\n");
  35.    return;
  36.    }
  37. if((infile = fopen( argv[1], (replace = (argc == 2)) ? "rb+" : "rb" )) == NULL) {
  38.    fputs("Could not open input file.\n", stdout);
  39.    return;
  40.    }
  41. if (replace)
  42.    outfile = infile;
  43. else if ((outfile = fopen( argv[2], "wb" )) == NULL) {
  44.    fputs("Could not open output file.\n", stdout);
  45.    fclose(infile);
  46.    return;
  47.    }
  48.  
  49. begin:
  50. err = 0;
  51. OUTBUFSIZ <<= 1;
  52. if ((buf = (signed char *)realloc(buf, OUTBUFSIZ)) == NULL) {
  53.    fputs("Not enough memory!\n", stdout);
  54.    goto cleanup;
  55.    }
  56.  
  57. while( (ch[p=plast] = fgetc(infile)) != EOF ) {
  58.    ++inpos;
  59.    plast = !p;
  60.    if( ch[p] == LF && ch[plast] != CR) {
  61.       buf[bufptr++] = CR;
  62.       if (bufptr == OUTBUFSIZ) err = writebuf();
  63.       if (err > 0) break;
  64.       }
  65.    buf[bufptr++] = ch[p];
  66.    if (err == -100) goto begin;
  67.    if (bufptr == OUTBUFSIZ) if ((err = writebuf()) > 0) break;
  68.    if (err == -100) goto begin;
  69.    }
  70.  
  71. while (!err && bufptr) {
  72.    inpos = outpos+bufptr;  /* reset to allow remaining writes */
  73.    err = writebuf();
  74.    }
  75.  
  76. if (err == 1)
  77.    fputs("Error writing output file.\n", stdout);
  78. else if (err == 2)
  79.    fputs("Error reading input file.\n", stdout);
  80. else if (replace) {
  81.    fputs(argv[1], stdout);
  82.    fputs(" now has CR+LF end-of-line codes.\n", stdout);
  83.    }
  84. else {
  85.    fputs("File ", stdout);
  86.    fputs(argv[2], stdout);
  87.    fputs(" successfully created with CR+LF end-of-line codes.\n", stdout);
  88.    }
  89.  
  90. cleanup:
  91. free(buf);
  92. fclose(infile);
  93. if (!replace) fclose(outfile);
  94. }
  95.  
  96.  
  97. /* Write the buffer to the output file, when full or when done.
  98.  * Stop writing when end of buffer is hit, or, if we are in replace mode,
  99.  * when current input pointer is hit. */
  100. int writebuf()
  101. {
  102. unsigned short i = 0, j = 0, buflimit;
  103. if (replace) {
  104.    if ((buflimit = inpos - outpos) <= 0) return -100;
  105.    if (fseek(outfile, outpos, 0)) return 1;
  106.    }
  107. else
  108.    buflimit = bufptr;
  109. while (i < buflimit)
  110.    if (fputc(buf[i++], outfile) == EOF) return 1;
  111. outpos += buflimit;
  112. if (replace) {
  113.    if (fseek(infile, inpos, 0)) return 2;
  114.    for (i = buflimit; i < bufptr;) buf[j++] = buf[i++];
  115.    }
  116. bufptr -= buflimit;
  117. return 0;
  118. }
  119.